package ay.controller;

import ay.dto.MoodDTO;
import ay.model.Mood;
import ay.service.MoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

@Controller
@RequestMapping("/mood")
public class MoodController {

    @Autowired
    private MoodService moodService;

    @RequestMapping("/findAll")
    public String findAll(Model model){
        List<MoodDTO> moodDTOList = moodService.findAll();
        model.addAttribute("moods",moodDTOList);
        return "mood";
    }


//      ---传统点赞实现---
//    @GetMapping("/{moodId}/praise")
//    public String praise(Model model, @PathVariable(value = "moodId") String moodId,
//                         @RequestParam(value = "userId") String userId){
//        boolean isPraise = moodService.praiseMood(userId, moodId);
//
//        List<MoodDTO> moodDTOList = moodService.findAll();
//        model.addAttribute("moods",moodDTOList);
//        model.addAttribute("isPraise",isPraise);
//        return "mood";
//    }


//     ---redis点赞实现---
    @GetMapping("/{moodId}/praiseForRedis")
    public String praiseForRedis(Model model, @PathVariable(value = "moodId") String moodId,
                         @RequestParam(value = "userId") String userId){
        Random random = new Random();
        userId = random.nextInt(100)+"";

        boolean isPraise = moodService.praiseMoodForRedis(userId, moodId);

        List<MoodDTO> moodDTOList = moodService.findAll();
        model.addAttribute("moods",moodDTOList);
        model.addAttribute("isPraise",isPraise);
        return "mood";
    }


}
